package com.jelly.web.modules.monitor.log.service.impl;

import com.jelly.common.util.DateUtils;
import com.jelly.common.util.StringUtils;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.monitor.log.dao.OperLogAndUserMapper;
import com.jelly.web.modules.monitor.log.dao.OperLogMapper;
import com.jelly.web.modules.monitor.log.domain.OperLog;
import com.jelly.web.modules.monitor.log.domain.OperLogAndUser;
import com.jelly.web.modules.monitor.log.domain.OperLogAndUserExample;
import com.jelly.web.modules.monitor.log.domain.OperLogExample;
import com.jelly.web.modules.monitor.log.service.IOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-08-17 14:42
 * @功能描述: 操作日志记录接口实现类
 **/
@Service
public class OperLogServiceImpl implements IOperLogService {

    @Autowired
    private OperLogMapper operLogMapper;

    @Autowired
    private OperLogAndUserMapper operLogAndUserMapper;

    /**
     * 新增操作日志记录
     * @param operLog
     * @return
     */
    @Override
    public boolean insterOperLog(OperLog operLog) {
        operLog.setLogId(UUID.randomUUID().toString().replaceAll("-", ""));
        operLog.setAddDate(new Date());
        return operLogMapper.insertSelective(operLog) > 0;
    }

    /**
     * 获取操作日志列表
     * @param OperLogAndUser
     * @return
     */
    @Override
    public List<OperLogAndUser> getOperLog(OperLogAndUser operLogAndUser) {
        OperLogAndUserExample example = new OperLogAndUserExample();
        OperLogAndUserExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotBlank(operLogAndUser.getUserName())){
            criteria.andUserNameEqualTo(operLogAndUser.getUserName());
        }

        if(StringUtils.isNotBlank(operLogAndUser.getIsState())){
            criteria.andIsStateEqualTo(operLogAndUser.getIsState());
        }
        if(StringUtils.isNotBlank(operLogAndUser.getStartTime()) && StringUtils.isNotBlank(operLogAndUser.getEndTime())){

            criteria.andAddDateGreaterThanOrEqualTo(DateUtils.string2Date(operLogAndUser.getStartTime()));
            criteria.andAddDateLessThanOrEqualTo(DateUtils.string2Date(operLogAndUser.getEndTime()));
        }

        return operLogAndUserMapper.selectByExample(example);
    }

    /**
     * 获取当前用户操作日志列表
     * @param userId
     */
    @Override
    public List<OperLog> getOperLogUserId() {

        OperLogExample example = new OperLogExample();
        OperLogExample.Criteria criteria = example.createCriteria();

        criteria.andAddIdEqualTo(TokenUtil.getUserId());

        criteria.andAddDateGreaterThanOrEqualTo(DateUtils.getStartTime());
        criteria.andAddDateLessThanOrEqualTo(DateUtils.getnowEndTime());

        example.setOrderByClause("add_date desc");

        return operLogMapper.selectByExample(example);

    }

}
